<template>
  <el-row type="flex" justify="center" align="middle">
    <el-col :span="8"
      ><div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" @click="submitForm('ruleForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div></el-col
    >
  </el-row>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3到 12 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      //表单校验
      //valid为true或false
       this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
           let res=await axios.post("http://localhost:8888/api/private/v1/login", this.ruleForm)
              if (res.data.meta.status == 200) {
                //token是否登录
                localStorage.setItem('token',res.data.data.token)


                this.$message({//注意这个this必须使用箭头函数
                  message: "登录成功",
                  type: "success",
                  duration:800
                });
                //跳转到home主页

                this.$router.push('/home')
              } else {
                this.$message({
                  message: "重置成功",
                  type: "success",
                  duration:800
                });
              }
        } else {
          console.log("现在可以开始登录");
        }
      });
    },
    resetForm() {
      //resetFields()对输入的内容重置
      this.$refs.ruleForm.resetFields();
    },
  },
};
//三个部分 组件部分 js部分 css部分
//scoped解决样式共享问题
</script>

<style scoped>
.el-row{
  width: 100%;
  height: 100%;
    background-color: #2d434c;
}
.el-col {
  background-color: #fff;
  padding: 50px 0;
  border-radius: 15px;
}



</style>
